package org.csu.mypetstore.web.servlets;

import com.sun.org.apache.xpath.internal.operations.Or;
import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Cart;
import org.csu.mypetstore.domain.Order;
import org.csu.mypetstore.service.LogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class NewOrderFormServlet extends HttpServlet {

    private static final String SIGNON_FORM = "/WEB-INF/jsp/account/SignonForm.jsp";
    private static final String NEW_ORDER = "/WEB-INF/jsp/order/NewOrderForm.jsp";
    private static final String ERROR = "/WEB-INF/jsp/common/Error.jsp";


    private Order order;
    boolean shippingAddressRequired;
    boolean confirmed;
    List<Order> orderList;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();

//        AccountActionBean accountBean = (AccountActionBean) session.getAttribute("/actions/Account.action");
        Account account = (Account) session.getAttribute("account");
        Boolean authenticated = (Boolean) session.getAttribute("authenticated");
//        CartActionBean cartBean = (CartActionBean) session.getAttribute("/actions/Cart.action");
        Cart cart = (Cart) session.getAttribute("cart");


//        clear();
        order = new Order();
        shippingAddressRequired = false;
        confirmed = false;
        orderList = null;

        session.setAttribute("order", order);
        session.setAttribute("shippingAddressRequired", shippingAddressRequired);
        session.setAttribute("confirmed", confirmed);
        session.setAttribute("orderList", orderList);

        //没有处于登录状态
        if (account == null || !authenticated) {
            String message = "You must sign on before attempting to check out.  Please sign on and try checking out again.";
            session.setAttribute("message", message);

//            return new ForwardResolution(AccountActionBean.class);

            account = (Account) session.getAttribute("account");
            if(account != null){
                HttpServletRequest httpRequest= request;
                String strBackUrl = "http://" + request.getServerName() + ":" + request.getServerPort()
                        + httpRequest.getContextPath() + httpRequest.getServletPath() + "?";

                LogService logService = new LogService();
                String logInfo = logService.logInfo(" ") + strBackUrl + SIGNON_FORM;
                logService.insertLogInfo(account.getUsername(), logInfo);
            }
            request.getRequestDispatcher(SIGNON_FORM).forward(request, response);
        } else if (cart != null) {
            order.initOrder(account, cart);
            session.setAttribute("order", order);
//            return new ForwardResolution(NEW_ORDER);
            account = (Account) session.getAttribute("account");
            if(account != null){
                HttpServletRequest httpRequest= request;
                String strBackUrl = "http://" + request.getServerName() + ":" + request.getServerPort()
                        + httpRequest.getContextPath() + httpRequest.getServletPath() + "?";

                LogService logService = new LogService();
                String logInfo = logService.logInfo(" ") + strBackUrl + NEW_ORDER;
                logService.insertLogInfo(account.getUsername(), logInfo);
            }
            request.getRequestDispatcher(NEW_ORDER).forward(request, response);
        } else {
            String message = "An order could not be created because a cart could not be found.";
            session.setAttribute("message", message);

//            return new ForwardResolution(ERROR);
            account = (Account) session.getAttribute("account");
            if(account != null){
                HttpServletRequest httpRequest= request;
                String strBackUrl = "http://" + request.getServerName() + ":" + request.getServerPort()
                        + httpRequest.getContextPath() + httpRequest.getServletPath() + "?";

                LogService logService = new LogService();
                String logInfo = logService.logInfo(" ") + strBackUrl + ERROR;
                logService.insertLogInfo(account.getUsername(), logInfo);
            }
            request.getRequestDispatcher(ERROR).forward(request, response);
        }

    }
}
